$(function () {
	var cid = "1111";
	window.onload = function(){
		var bodyHeight = $("body").get(0).offsetHeight;
        var bodyWidth = $("body").get(0).offsetWidth;

        $(".main-body").css("height", (parseFloat(bodyHeight) - parseFloat(bodyWidth) / 10 * 1.05) + "px");
        
        initEvent();
        loadData();
	}
	
	//组装数据
	window.initData = function(data){
		cid = data.cid;
		sessionStorage.setItem("cid",cid);
		$("#title").text(data.name);
		$("#tqrTtle").text(data.name);
		$("#limitWeight").html(data.minPkg + "-" + data.maxPkg);
		$("#limitNum").html(data.minMail);
		$("#qrLimitWeight").html(data.minPkg + "-" + data.maxPkg);
		$("#qrLimitNum").html(data.minMail);
		var minPriceNote = data.minPriceNote;
		var lowToDiv = minPriceNote.split("#")[0].replace("**","<span>") + "</span>"
		var unitQtyDiv = minPriceNote.split("#")[1];
		$(".main-img").css("background-image","url("+data.imgUrl+")");
		$(".low-to-div").html(lowToDiv);
		$(".unit-qty-div").html(unitQtyDiv);
		var pensent = parseInt(parseFloat(data.curNum/data.needNum) * 100);
		$(".sign-progress-now").css("width",pensent+"%");
		$(".sign-progress-num").text(pensent+"%");
		$("#lastNum").text(data.needNum - data.curNum);
		var endTime = new Date(parseInt(data.endTmLong));
		$("#endTm").text(endTime.format("MM月dd日"));
		
		var note = data.note;
		var content = '';
		for(var i = 0 ; i < note.split("#").length ; i ++){
			var splitNote = note.split("#")[i];
			while(splitNote.indexOf("*") > -1){
				splitNote = splitNote.replace("*","<span>");
			}
			while(splitNote.indexOf("&") > -1){
				splitNote = splitNote.replace("&","</span>");
			}
			content += '<div>'+ splitNote;
		}
		$(".use-requirements-content").html(content);
		
		var width = 0.8 * data.imgs.length;
		var imgContent = '<div style="width:'+width+'rem">';
		for(var i = 0 ; i < data.imgs.length ; i ++){
			imgContent += '<img src="'+data.imgs[i]+'">';
		}
		imgContent += "</div>"
		$(".signed-info-img").html(imgContent);
		$("#signedNum").text(data.curNum);
	}
	
	//绑定事件
	window.initEvent = function(){
		$(".sign-btn").click(function(){
			window.location.href="../delivery/delivery.html";
		});
		$(".share").click(function(){
			$(".qrcode-div").show();
			var url = "http://10.2.4.81:8080/marathon-front/page/main/?cid="+cid;
			showQrcode($(".qrcode-div"),url,$("#qrImg"),$("#title").text(),$(".qrcode-main"),4,0.6)
		})
		$(".qrcode-back").click(function(){
			$(".qrcode-div").hide();
		})
	}
	//请求后台
	window.loadData = function(){
//		var data = {
//			"cid":"1111",
//		    "name":"鞋服专送120601期",
//		    "minMail":20,
//		    "minPkg":1.5,
//		    "maxPkg":5,
//		    "minPriceNote":"低至￥ **6.#15KG",
//		    "imgUrl":"img/logo.jpg",
//		    "needNum":20,
//		    "curNum":1,
//		    "note":"1.每日承诺寄件量不低于*20&件（周六日及法定假期不要求承诺量）日发件不足20件或运费不足*120&元将按照120元收取最低运作费用。#2.承诺使用周期内提前终止寄件视为违约行为。#3.集货周期内未达到目标则自动取消集货。#4.用户需自行完成打包并贴好面单。#5.每日在约定时间进行1次收件，不支持随寄随收。#6.按月结运费。不支持现寄现结。",
//		    "endTmLong":1513324892541
//		};
//		var imgs = [];
//		for(var i = 0 ; i < 12 ;i ++){
//			imgs.push("img/header_logo.png");
//		}
//		data.imgs = imgs;
//		initData(data);
//		wxShare(data);
//		return;
		$.ajax({
            url: "http://10.2.4.130:8888/services/collectInfo/selectCollectInfo",
            type: "get",
            data: "cid=" + cid ,
			dataType: 'json',
			xhrFields: {  
                'Access-Control-Allow-Origin': '*'
            }, 
            success: function (data) {
                if (data && data.success) {
                	initData(data.obj);
                }
            },
            error: function (data) {
                
            }
        });
	}
	
	//微信分享功能
	window.wxShare = function(data){
		//分享信息
        shareInfo = {
        	title : data.name,
        	desc : data.name,
        	link : basePath + "/page/main/index.html?cid="+data.cid,
        	imgUrl : data.imgUrl
        };
        
    	//分享微信好友
        wx.onMenuShareAppMessage({    
            title: shareInfo.title,    
            desc: shareInfo.desc,    
            link: shareInfo.link,    
            imgUrl: shareInfo.imgUrl
        });
        //分享朋友圈
        wx.onMenuShareTimeline({    
            title: shareInfo.title,    
            desc: shareInfo.desc,    
            link: shareInfo.link,    
            imgUrl: shareInfo.imgUrl
        });
	}
});